package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.trade.CrossUnionTradeMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.C_UNION_TRADE_ROUTE_OPEN, desc = "开启路线")
public class CrossTradeRouteOpenCmd extends CrossCommand {

    int routeId;
    long playerId;
    String consume;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionTradeProto.RouteOpenCrossReqMsg reqMsg = UnionTradeProto.RouteOpenCrossReqMsg.parseFrom(packet.getBytes());
        routeId = reqMsg.getRouteId();
        consume = reqMsg.getConsume();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        ret = CrossUnionTradeMgr.openTradeRoute(serverId, playerId, routeId, consume);

    }

    @Override
    public void sendData() {
        if(ret != 0){
            if(StringUtils.isNullOrEmpty(consume)){
                UnionTradeProto.RouteOpenRespMsg.Builder routeOpenResp = UnionTradeProto.RouteOpenRespMsg.newBuilder();
                routeOpenResp.setRet(ret);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_TRADE_ROUTE_OPEN, routeOpenResp));
            }else {
                UnionTradeProto.RouteOpenCrossRespMsg.Builder crossResp = UnionTradeProto.RouteOpenCrossRespMsg.newBuilder();
                crossResp.setRet(ret);
                crossResp.setRouteId(routeId);
                crossResp.setConsume(consume);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_UNION_TRADE_ROUTE_OPEN_FROM_CROSS, crossResp));
            }
        }
    }
}
